package com.lu.netty.io.server

import com.lu.netty.io.core.NettyConfigure
import io.netty.channel.ChannelInitializer
import io.netty.channel.socket.SocketChannel
import io.netty.handler.codec.string.StringDecoder
import io.netty.handler.codec.string.StringEncoder
import io.netty.handler.timeout.IdleStateHandler
import java.util.concurrent.TimeUnit

class ServerChannelHandler(private val adapterCallback: IServerAdapterCallback) :
    ChannelInitializer<SocketChannel>() {

    override fun initChannel(ch: SocketChannel?) {
        ch?.let { channel ->
            val pipeline = channel.pipeline()
            pipeline.addLast(
                IdleStateHandler(
                    NettyConfigure.readIdleTime,
                    NettyConfigure.writeIdleTime,
                    NettyConfigure.allIdleTime,
                    TimeUnit.MILLISECONDS
                ),
//                DelimiterBasedFrameDecoder(1024, Unpooled.copiedBuffer("###".toByteArray())),
                StringEncoder(),
                StringDecoder(),
                ServerCHAdapter("Server", adapterCallback),
            )
        }
    }
}